/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.core.components;

import com.chasekernan.hxnova.core.components.ComponentSet;
import haxe.xml.Check;

/**
    Includes both regular and penetrating scanners.
**/
class Scanner extends Component {
    
    public var range(default, setRange) : Int;
    private function setRange(r : Int) : Int {if(r < 0) {throw "Range must be >= 0";} else {range = r;} return range;}
    
    /**
    * penetrating range is 0 if it isn't penetrating
    */
    public var penetratingRange(default, setPRange) : Int;
    private function setPRange(pr : Int) : Int {if(pr < 0) {throw "Pen range must be >= 0";} else {penetratingRange = pr;} return penetratingRange;}
    
    public function new(name : String, ?range : Int, ?penetratingRange : Int) {
        super(name);
        setRange(if(range == null) 0 else range);
        setPRange(if (penetratingRange == null) 0 else 
                penetratingRange);
    }
    
    /**
        Returns true if the scanner's penetrating range != 0.
    **/
    public function isPenetrating() : Bool {
        return penetratingRange != 0;
    }
    
    public static function writeToXml(s : Scanner) : Xml {
        return Component.writeToXml(s);
    }
    
    public static function readFromXml(xml : Xml) : Scanner {
        try {
            var s = new Scanner("");
            //Check.checkNode(xml, s.getRules());
            s.generateFromComponentXml(xml, s);
            
            return s;
        } catch (e : Dynamic) {
            throw 'Bad xml file for scanner: ' + e;
        }
    }
}